package com.audaque.springboot.foshanupload.reactor.webfluxdemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.ReactiveValueOperations;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping(value = "/stringRedisReactive")
public class StringRedisReactiveController {

    @Autowired
    private ReactiveRedisTemplate reactiveRedisTemplate;

    @GetMapping(value = "/{id}")
    public Mono<String> findStringById(@PathVariable("id") Long id) {
        String key = "string_" + id;
        ReactiveValueOperations<String, String> operations = reactiveRedisTemplate.opsForValue();
        Mono<String> string = operations.get(key);
        return string;
    }

    @PostMapping
    public Mono<String> saveString(@RequestBody String id) {
        String key = "string_" + id;
        ReactiveValueOperations<String, String> operations = reactiveRedisTemplate.opsForValue();
        return operations.getAndSet(key, id);
    }

    @DeleteMapping(value = "/{id}")
    public Mono<Long> deleteString(@PathVariable("id") Long id) {
        String key = "string_" + id;
        return reactiveRedisTemplate.delete(key);
    }
}
